<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/21 0021
 * Time: 17:14
 */

namespace app\controllers;
use app\common\base\BaseController;
use app\models\SwfArea;
use app\models\TbArticle;
use app\models\TbFriend;
use app\models\TbUser;
use app\models\UploadForm;
use Faker\Provider\Base;
use Yii;
use yii\base\Exception;
use yii\web\Controller;
use yii\web\Cookie;
use yii\web\UploadedFile;

class UserController extends BaseController
{

    /**
     * 加载个人中心页
     * @return string
     */
    public function actionCenter(){
        $username = Yii::$app->session->get("regname");//文章作者
        $user=TbUser::selectRegname($username);
        $user_id=$user['id'];
        $friend=TbFriend::selectU_ID($user_id);
        $article=TbArticle::selecttop5($username);
        list($articleall,$pagination) =TbArticle::selectnameAll($username);
        return $this->render("center",[
            "top5article"=>$article,
            "articleall"=>$articleall,
            "pagination"=>$pagination,
            'user'=>null,
            'friend'=>$friend
        ]);
    }

    /**
     * 访问好友/名人个人中心主页
     * @param $regname
     * @return string
     */
    public function actionFriend($regname){
        $user =TbUser::selectRegname($regname);
        $article=TbArticle::selecttop5($regname);
        list($articleall,$pagination) =TbArticle::selectnameAll($regname);
        return $this->render("center",[
            "top5article"=>$article,
            "articleall"=>$articleall,
            "pagination"=>$pagination,
            'user'=>$user,
            'friend'=>null
        ]);
    }

    /**
     * 添加好友方法
     * @return string
     */
    public function actionAddFriend(){
        $tbfriend = new TbFriend();
        $regname = $this->getParam('regname');
        $user = TbUser::selectRegname($regname);
        $username = Yii::$app->session->get("regname");
        $users=TbUser::selectRegname($username);
        $user_id=$users['id'];
        $name = $user['regname'];
        $tbfriend->u_id = $user_id;
        $tbfriend->name = $name;
        if ($user_id == null || $name ==null){
            return $this->out(false,'添加好友失败');
        }
        $selecName=TbFriend::selectName($regname);
        $friendname=$selecName['name'];
        if ( $friendname == $regname){
            return $this->out(false,'好友已存在');
        }
        if ($tbfriend->save()){
            return $this->out(true,'添加好友成功',$regname);
        }
        return $this->out(false,'添加好友失败');
    }
    /**
     * 加载基本资料页
     * @return string
     */
    public function actionInformation(){
        $year_start = 1980;
        $year_end = date('Y');
        $regname=Yii::$app->session->get('regname');
        $user = TbUser::selectRegname($regname);
        $selectcityall=SwfArea::selectCityAll();
        list($selectcityid,$selcetcityparentid) = SwfArea::selectCityId($user['regcity'],$user['regcityparent']);

        $model =new UploadForm();
        if(Yii::$app->request->isPost){
            $model->file =UploadedFile::getInstance($model,'file');
            $name=$model->file->baseName .','.$model->file->extension;
            Yii::trace($name);
            if ($model->file && $model->validate()){
                $model->file->saveAs('./image/'.$name);
            }
        }
        return $this->render('information', [
            'selectcityall'=>$selectcityall,
            'selectRegname'=>$user,
            'regbirthday'=>explode("-", $user['regbirthday']),
            'selectcityid'=>$selectcityid,
            'selcetcityparentid'=>$selcetcityparentid,
            'year_start'=>$year_start,
            'year_end'=>$year_end,
            'model'=>$model
        ]);
    }

    /**
     * 城市二级联动处理
     * @return string
     */
    public function actionCity(){
        try{
            $selectcityparent_id=Yii::$app->request->post('province');
            if ($selectcityparent_id==null) return json_encode($arr=array('success'=>0));
            $selectcityparent_id_all=SwfArea::selectCityParent_id($selectcityparent_id);
            return $this->out(true,'加载成功',$selectcityparent_id_all);
        }catch (Exception $e){
            return $this->out(false,'加载失败');
        }
    }

    /**
     * 个人资料修改
     * @return \yii\web\Response
     */
    public function actionInformationSubmit(){
        $regrealname = $this->getParam('regrealname');//接收姓名
        $sex=$this->getParam('sex');//接收性别  1：男 2：女 3：保密
        $birthday_y=$this->getParam('birthday_y');//接收年
        $birthday_m=$this->getParam('birthday_m');//接收月
        $birthday_d=$this->getParam('birthday_d');//接收日
        $birthday=$birthday_y."-" .$birthday_m. "-" .$birthday_d; //年月日拼接
        $province=$this->getParam('province');//接收省
        $city=$this->getParam('city');//接收市 没有为0
        $regintroduce=$this->getParam('regintroduce');//接收介绍
        $regqq=$this->getParam('regqq');//接收QQ
        $regmobile=$this->getParam('regmobile');//接收手机
        $regemail=$this->getParam('regemail');//接收邮箱
        $reghomepage=$this->getParam('reghomepage');//接收个人主页

        $model =new UploadForm();
        if(Yii::$app->request->isPost){
            $model->file =UploadedFile::getInstance($model,'file');
            if ($model->file && $model->validate()){
                $model->file->saveAs('./image/name',$model->file->baseName .','.$model->file->extension);
            }
        }
        //数据取值如果为""，那么赋值为空
        if($province == 0) $province=null;
        else if ($city == 0 && $province == 0)$city=null;
        $attributes=array('regrealname'=>$regrealname,'regsex'=>$sex,'regbirthday'=>$birthday,
            'regcity'=>$province,'regintroduce'=>$regintroduce,'regqq'=>$regqq,'regmobile'=>$regmobile,
            'regemail'=>$regemail,'reghomepage'=>$reghomepage,'regcityparent'=>$city);
        $regname=Yii::$app->session->get('regname');
        $params=array('regname'=>$regname);
        TbUser::updateInformation($attributes,$params);
        return $this->redirect(['user/center']);
//        else if ($regintroduce=="")$regintroduce=null;
//        else if ($regqq=="")$regqq=null;
//        else if ($regmobile=="")$regmobile=null;
//        else if ($regemail=="")$regemail=null;
//        else if ($reghomepage=="")$reghomepage=null;
    }

}